<?php

/**
* @current headline.php
* @author   LiuYang <liu_yang@haibao.com>
* @date 2017年2月27日 下午5:08:42
*/

namespace haibao\jiepai\data;
use \haibao\jiepai\model\filter\focus\CmsFocusMaterial as CmsFocusMaterialFilter;
class HeadLine extends \haibao\jiepai\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\jiepai\model\data\focus\CmsFocusMaterial');
    }
    
    public function addHeadlineData($addHeadLineModel){
        $this->beginTranscation();
        try{
            $this->add($addHeadLineModel);
        } catch(\Exception $e){
            throw $e;
        }
        $this->endtranscation();
        return $addHeadLineModel;
    }
    
    public function updateHeadLineData($editHeadLineModel){
        $headLineModel = $this->packHeadLineFilter($editHeadLineModel);
        $this->beginTranscation();
        try{
            $this->updateById($editHeadLineModel->Id,$headLineModel);
        } catch(\Exception $e){
            throw $e;
        }
        $this->endtranscation();
    }
    private function packHeadLineFilter($editHeadLineModel){
        $cmsHeadLinesFilter = new CmsFocusMaterialFilter();
        $cmsHeadLinesFilter = array(
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_CATEGORY =>$editHeadLineModel->Category,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_TYPE => $editHeadLineModel->Type,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_TYPEID => $editHeadLineModel->TypeID,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_LINK => $editHeadLineModel->Link,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_TITLE => $editHeadLineModel->Title,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_PICTURE => $editHeadLineModel->Picture,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_CREATETIME => $editHeadLineModel->CreateTime,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_UPTIME => $editHeadLineModel->UpTime,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_DOWNTIME => $editHeadLineModel->DownTime,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_EDITOR => $editHeadLineModel->Editor,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_REMARKS => $editHeadLineModel->Remarks,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_UPINDEX => $editHeadLineModel->UpIndex,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_STATUS => $editHeadLineModel->Status,
            CmsFocusMaterialFilter::CONDITION_FIELD_NAME_FLAG => $editHeadLineModel->Flag,
            
        ); 
        return $cmsHeadLinesFilter;
    }
    public function getArticleInfoById($id){
        $sql = "SELECT art.ArticleID,art.Title,con.ContentInfo 
                FROM cms_articles art LEFT JOIN cms_article_content con ON art.ArticleID = con.ArticleID 
                WHERE art.ArticleID = %s AND con.Type=6;";
        $result = $this->query($sql,array($id));
        $data = array();
        while ($row = $result->fetch_assoc()){
            $data[$row['ArticleID']] = $row;
        }
        return $data;
    }
    
    //检查是否已存在该位置的信息流-头条
    public function cmsCheckInfoHeadline($focusId,$index,$UpTime,$DownTime){
        if($DownTime=='0000-00-00 00:00:00'){//未设置下架时间
            $sql = "SELECT count(*) as count from cms_focus_material WHERE Flag=0 AND (DownTime>'".$UpTime.
            "' OR DownTime='0000-00-00 00:00:00') AND UpIndex=%s AND Category=2 ";
        }else{
            $sql = "SELECT count(*) as count from cms_focus_material WHERE Flag=0 AND ( (DownTime='0000-00-00 00:00:00' AND UpTime<'".$DownTime."'
             ) OR (UpTime < DownTime AND DownTime > NOW() AND 
             (!(UpTime>'".$DownTime."') AND !(DownTime<'".$UpTime."')))
             )  AND UpIndex=%s AND Category=2 ";
        }
        if($focusId){
            $sql .= " AND ID!=".$focusId;
        }
        $result = $this->query($sql,array($index));
        return $result->fetch_assoc();
    }
    public function cmsDownFocus($focusId){
        $sql = "UPDATE cms_focus_material SET DownTime='".date('Y-m-d H:i:s')."' WHERE ID=".$focusId;
        $this->query($sql);
    }
}
